package lv.rtu.ditf.dc.gui.listener;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import lv.rtu.ditf.dc.gui.DocumentCategorizationForm;

/**
 * 
 * @author Pāvels Koliškins, Edgars Matisāns
 * Document listeners are used to determine when to lock/unlock GUI components
 */
public class CustomDocumentListener implements DocumentListener {

    private DocumentCategorizationForm jFrame;

    public CustomDocumentListener() {}
    public CustomDocumentListener(DocumentCategorizationForm jFrame) {
        this.jFrame = jFrame;
    }

    public void insertUpdate(DocumentEvent e) {
        triggerOnContentChange(e);
    }

    public void removeUpdate(DocumentEvent e) {
        triggerOnContentChange(e);
    }

    public void changedUpdate(DocumentEvent e) {
        triggerOnContentChange(e);
    }

    private void triggerOnContentChange(DocumentEvent e) {
        try {
            String documentText = e.getDocument().getText(0, e.getDocument().getLength()).trim();
            if (documentText.length() > 0) {
                this.jFrame.lockSaveButton(false);
            } else {
                this.jFrame.lockSaveButton(true);
            }
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }

}

